<template>
  <div class="login">
    <header-tab pageType="1"></header-tab>
    <el-card class="box-card">
      <h2>登录</h2>
      <el-form
          :model="ruleForm"
          status-icon
          :rules="rules"
          ref="ruleForm"
          label-position="left"
          label-width="70px"
          class="login-from"
      >
        <el-form-item label="账号" prop="username">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
              type="password"
              v-model="ruleForm.password"
              autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <div class="btnGroup">
        <el-button
            type="primary"
            @click="submitForm('ruleForm')"
        >登录(首次注册见右上角)
        </el-button>
<!--        <router-link to="/gasoline" class="forget">忘记密码</router-link>-->
      </div>
    </el-card>
  </div>
</template>

<script>
import HeaderTab from "@/components/HeaderTab"
export default {
  name: "Login",
  components: {
    HeaderTab
  },
  data() {
    return {
      ruleForm: {
        username: "",
        password: ""
      },
      rules: {
        username: [
          {required: true, message: "用户名不能为空！", trigger: "blur"},
        ],
        password: [
          {required: true, message: "密码不能为空！", trigger: "blur"},
        ]
      }
    }
  },
  methods: {
//     loading() {
//       alert("请先登录")
//     },
//     submitForm(formName){
//       this.$refs[formName].validate((valid) => {
//         if (valid){
//           this.$router.push({path: '/home'});
//           this.$message(res.message)
//           console.log(res)
//         }else {
//           this.$message(res.message)
//         }
//       })
//
//       })
// }

    submitForm () {
      let data = {
        username: this.ruleForm.username,
        password: this.ruleForm.password
      }
      this.$store.dispatch("Login", data)
          .then((res) => {
            this.$router.push({path: '/home'});
            this.$message(res.message)
              console.log(res)
          })
          .catch(() => {
            this.loading = false;
          });
    }
  }
}
</script>

<style>

.login {
  overflow: hidden;
}

.box-card {
  margin: 80px auto;
  width: 400px;
  padding:10px;
}

h2{
  text-align: center;
}
/* 设置登录面板中的表单居中 */
.login-from {
  margin: auto auto;
}
.btnGroup{
  width: 100%;
  margin-left: 100px;
}
.btnGroup button{
  float: left;
}
.forget{
  float: left;
  margin-left: 10px;
  font-size: 15px;
  color: #409EFF;
  margin-top: 5px;
  display: block;
}

</style>
